import {
  createWebHashHistory
} from "vue-router";
import {
  createRouter
} from "vue-router";
import storage from "../utils/storage";

const asyncRoutes = [];
const files =
  import.meta.glob("@/router/modules/*.js", {
    eager: true
  });
for (const f in files) {
  const o = files[f].default;
  asyncRoutes.push(o);
}
// console.log(asyncRoutes);

const routes = [{
    path: "/",
    component: () => import("@/view/login/index.vue"),
    meta: {
      title: "登录",
    },
  },
  {
    path: "/login",
    component: () => import("@/view/login/index.vue"),
    meta: {
      title: "登录",
    },
  },
  {
    path: "/main",
    component: () => import("@/layout/index.vue"),
    children: asyncRoutes,
  },
  // 路内停车订单详情
  {
    path: "/order/road/process/details",
    meta: {
      title: "进行中订单详情",
    },
    component: () =>
      import(
        "@/view/order/roadsideOrders/roadsideOngoingOrders/components/index.vue"
      ),
  },
  {
    path: "/order/road/finished/details",
    meta: {
      title: "已完成订单详情",
    },
    component: () =>
      import(
        "@/view/order/roadsideOrders/roadsideCompletedOrder/components/index.vue"
      ),
  },
  {
    path: "/order/road/unpaid/details",
    meta: {
      title: "待补缴订单详情",
    },
    component: () =>
      import(
        "@/view/order/roadsideOrders/outstandingOrders/components/index.vue"
      ),
  },
  // 停车场停车订单详情
  {
    path: "/order/park/process/details",
    meta: {
      title: "进行中订单详情",
    },
    component: () =>
      import(
        "@/view/order/cparkOrders/cparkOngoingOrders/components/index.vue"
      ),
  },
  {
    path: "/order/park/finished/details",
    meta: {
      title: "已完成订单详情",
    },
    component: () =>
      import(
        "@/view/order/cparkOrders/cparkCompletedOrder/components/index.vue"
      ),
  },
  // 场库信息详情
  {
    path: "/park/manage/bInfo/details",
    meta: {
      title: "场库信息详情"
    },
    component: () => import("@/view/park/manage/bInfo/components/index.vue")
  },
  {
    path: "/officialWebsite",
    component: () => import("@/view/officialWebsite/index.vue"),
    meta: {
      title: "官网"
    },
  },
];

const router = createRouter({
  routes,
  history: createWebHashHistory(),
});
const whiteRoute = ["/", "/login"];
router.beforeEach((to, from, next) => {
  const token = storage.get("access_token");
  if (whiteRoute.includes(to.path)) {
    next();
    return;
  }
  if (!token && !whiteRoute.includes(to.path)) {
    next("/");
    return;
  }
  next();
  // next({ ...to, replace: true });
});
router.afterEach((to, from, next) => {
  document.title = to.meta.title + " - 南城县智慧停车一体化平台";
});

export default router;

// export{
//   asyncRoutes
// }